from django.http import HttpResponse, JsonResponse
from django.utils.deprecation import MiddlewareMixin
from django.conf import settings
from django.shortcuts import redirect, render


class Context:
    def __init__(self, role, id, name):
        self.role = role
        self.id = id
        self.name = name


class AuthMiddleware(MiddlewareMixin):
    def is_white_url_by_path_info(self, request):
        white_url = ['/login/', '/sms/login/', '/send/sms/']
        if request.path_info in white_url:
            return True

    def is_white_url_by_name(self, request):
        white_url = ['login', 'sms_login', 'send_sms']
        url_name = request.resolver_match.url_name
        if url_name in white_url:
            return True

    def process_request(self, request):
        # return
        # white url list

        if self.is_white_url_by_path_info(request):
            return None
        data_dict = request.session.get(settings.NB_SESSION_KEY)

        if not data_dict:
            return redirect(settings.NB_LOGIN_NAME)
        request.nb_user = Context(**data_dict)

    def process_view(self, request, callback, *args, **kwargs):
        # white url list
        if self.is_white_url_by_name(request):
            return None
        data_dict = request.session.get(settings.NB_SESSION_KEY)

        if not data_dict:
            return redirect(settings.NB_LOGIN_NAME)
        request.nb_user = Context(**data_dict)


class AuthMiddlewarePathInfo(MiddlewareMixin):
    def is_white_url_by_path_info(self, request):
        if request.path_info in settings.NB_WHITE_URL:
            return True

    def process_request(self, request):
        # return
        # white url list
        if self.is_white_url_by_path_info(request):
            return
        data_dict = request.session.get(settings.NB_SESSION_KEY)
        if not data_dict:
            return redirect(settings.NB_LOGIN_NAME)
        request.nb_user = Context(**data_dict)

    # def process_response(self, request, response):
    #     if self.is_white_url_by_path_info(request):
    #         return response
    #
    #     data_dict = request.session.get(settings.NB_SESSION_KEY)
    #     if not data_dict:
    #         return redirect(settings.NB_LOGIN_NAME)
    #
    #     request.nb_user = Context(**data_dict)
    #     print(request.nb_user)
    #     return response


class PermissionMiddleWare(MiddlewareMixin):

    def is_ajax(self, request):
        return request.META.get('HTTP_X_REQUESTED_WITH') == 'XMLHttpRequest'

    def is_white_url_by_name(self, request):
        url_name = request.resolver_match.url_name
        if url_name in settings.NB_WHITE_PERMISSION_NAME:
            return True

    def process_view(self, request, callback, *args, **kwargs):
        # login,sms_login,send_sms
        if self.is_white_url_by_name(request):
            return
        #
        role = request.nb_user.role
        permission_dict = settings.NB_PERMISSIONS[role]
        url_name = request.resolver_match.url_name
        # text_list = []
        # current_dict = permission_dict.get(url_name)
        # text_list.insert(0, current_dict['text'])
        # parent = current_dict['parent']
        # while parent:
        #     loop = permission_dict[parent]
        #     text_list.insert(0, loop['text'])
        #     parent = loop['parent']
        # print(text_list)
        if url_name in permission_dict:
            return
        if self.is_ajax(request):
            return JsonResponse({'status': False, 'msg': "NO PERMISSION"})
        return render(request, '404.html')
